﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;

/// <summary>
/// IndexManager 的摘要说明
/// </summary>
public class IndexManager
{
    public static readonly IndexManager indexManage = new IndexManager();
    //请求队列 解决索引目录同时操作的并发问题
    private Queue<IndexQueueViewMode> indexQueue = new Queue<IndexQueueViewMode>();

    private IndexManager()
    {
    }

    #region 线程处理
    ////新开一个线程、用于处理队列中的数据
    //public void StartNewThread()
    //{
    //    ThreadPool.QueueUserWorkItem(new WaitCallback(QueueToIndex));
    //}
    //定义一个线程 将队列中的数据取出来 插入索引库中
    //private void QueueToIndex(object para)
    //{
    //    while (true)
    //    {
    //        if (indexQueue.Count > 0)
    //        {
    //            PayCompare();
    //        }
    //        else
    //        {
    //            Thread.Sleep(3000);
    //        }
    //    }
    //}

    private void PayCompare()
    {
        HuatICBCPayService.ICBCService payService = new HuatICBCPayService.ICBCService();
        HuatICBCPayService.ICBC payICBCModel = new HuatICBCPayService.ICBC();
        payService.CookieContainer = new System.Net.CookieContainer();
        if (payService.Verify("cet.huat.edu.cn", "8260147"))
        {
            while (indexQueue.Count > 0)
            {
                IndexQueueViewMode indexInfo = indexQueue.Dequeue();
                string orderId = "CET" + indexInfo.EmYear + indexInfo.EmNum + "-" + indexInfo.OrderID;
                DataTable dt = payService.GetOrderInfo(orderId);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["transtat"].ToString() == "1")
                    {
                        string[] str = orderId.Split(new char[] { '-' });
                        orderId = str[1];
                        if (Exam.Order_Complete(orderId))
                        {
                            BSUtility.WriteLog(orderId + "比对成功");
                        }
                    }
                }
                Thread.Sleep(1000);
            }
        }
    }
    public void Start()
    {
        BSUtility.WriteLog("----------系统自动对帐功能开始----------");
        if (indexQueue.Count > 0)
        {
            PayCompare();
        }
        else
        {
            BSUtility.WriteLog("没有需要对帐的数据");
        }
        BSUtility.WriteLog("----------系统自动对帐功能结束----------");
    }
    #endregion

    #region 索引队列操作
    /// <summary>
    /// 新增索引队列，默认插入最后一个队列
    /// </summary>
    /// <param name="books"></param>
    public void Add(IndexQueueViewMode iqvm)
    {
        indexQueue.Enqueue(iqvm);
    }
    #endregion
}

#region 索引队列模型
public class IndexQueueViewMode
{
    public string OrderID
    {
        get;
        set;
    }
    public string EmYear
    {
        get;
        set;
    }
    public string EmNum
    {
        get;
        set;
    }
    public string Abstract
    {
        get;
        set;
    }
}
#endregion